/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package candidate.beans;

import facades.CandidateEconomicTypeFacade;
import facades.CandidateFacade;
import facades.CandidateTypeFacade;
import facades.UserFacade;
import facades.UserTypeFacade;
import entities.Candidate;
import entities.User;
import util.ImageStoreHelper;
import util.Sha256Encrypter;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;
import javax.transaction.Transactional;
import qualifiers.CreatedEntityEvent;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class CreateCandidateBean implements Serializable {
private Candidate candidate;
@Inject
CandidateFacade candidateEJB;
@Inject
UserFacade userEJB;
@Inject
UserTypeFacade userTypeEJB;
@Inject
CandidateEconomicTypeFacade candidateEconomicTypeEJB;
@Inject
CandidateTypeFacade candidateTypeEJB;
@Inject
@CreatedEntityEvent
private Event<Candidate> newCandidateEvent;
@Inject
@CreatedEntityEvent
private Event<User> newUserEvent;

    /**
     * Creates a new instance of CreateCandidateBean
     */
    public CreateCandidateBean() {
        if(candidate==null){
            candidate=new Candidate();
        }
        if(candidate.getUser()==null){
            candidate.setUser(new User());
        }
    }

    @PostConstruct
    private void fillCandidateBean(){
        candidate.getUser().setUserType(userTypeEJB.find(1));
    }
    
    @Transactional
    public String createCandidate() throws IOException{
        persistCandidate();
        makeDirs();
        return "/views/candidate/NewEditCandidateView?faces-redirect=true";
    }
    
    public void makeDirs(){
        File imagesDir=new File(ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"images");
        File videosDir=new File(ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"videos");
        imagesDir.mkdirs();
        videosDir.mkdirs();
    }
    
    private void persistCandidate() throws IOException{
        Date date = new Date();
        //User user=candidate.getUser();
        candidate.getUser().setActive(true);
        candidate.getUser().setCreationDate(date);
        candidate.getUser().setLastDate(date);
        candidate.getUser().setLogins(0);
        candidate.setLastModificationDate(date);
        candidate.setVisits(0);
        candidate.setCandidateEconomicType(candidateEconomicTypeEJB.find(1));
        candidate.setCandidateType(candidateTypeEJB.find(1));
        
        try {
            candidate.getUser().setEmail(candidate.getUser().getEmail().toLowerCase());
            candidate.getUser().setPassword(Sha256Encrypter.getSha256(candidate.getUser().getPassword()));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(CreateCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(CreateCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        candidate.getUser().setCandidate(candidate);
        userEJB.create(candidate.getUser());
        newUserEvent.fire(candidate.getUser());
        try{
            newCandidateEvent.fire(candidate.getUser().getCandidate());
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    /**
     * @return the candidate
     */
    public Candidate getCandidate() {
        return candidate;
    }

    /**
     * @param candidate the candidate to set
     */
    public void setCandidate(Candidate candidate) {
        this.candidate = candidate;
    }
        
}
